//
//  LGTableView.m
//  LoveTravel
//
//  Created by MS on 16-1-21.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "LGTableView.h"
#import "Selection_RecommCell.h"
#import "Selection_CollectionCell.h"
#import "NSString+Size.h"
@interface LGTableView ()<UITableViewDelegate,UITableViewDataSource,Selection_CollectionCellDelegate>

@property (nonatomic,strong) NSMutableArray * selectionArray;

@end

@implementation LGTableView

-(void)setItemsArray:(NSArray *)itemsArray
{
    _itemsArray = itemsArray;
    for (RecommsData * tmp in itemsArray) {

        if ([tmp.type isEqualToString:@"Recomm"]) {
           
            CGSize size = [tmp.short_desc sizeWithString:CGSizeMake(self.width-20, MAXFLOAT) Font:14];
            tmp.short_descHeight = size.height;
            
        }
       
    }
    
    self.selectionArray = [NSMutableArray arrayWithArray: itemsArray];
    [self.tableView reloadData];
}


-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self addTableView];
    }
    return self;
}


-(void)addTableView
{
    UITableView * tabView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    self.tableView = tabView;
    [self addSubview:tabView];
    tabView.separatorColor = [UIColor clearColor];
    tabView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);

    tabView.delegate = self;
    tabView.dataSource = self;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.selectionArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *  cell = nil;
    
    RecommsData * recomm = _selectionArray[indexPath.row];
    
    if ([recomm.type isEqualToString:@"Recomm"]) {
        Selection_RecommCell *  cell0 = [Selection_RecommCell cellWithtableView:tableView];
        cell0.recomData = recomm;
        cell = cell0;
    }
    else{
        Selection_CollectionCell * cell1 = [Selection_CollectionCell cellWithtableView:tableView];
        cell1.delegate = self;
        cell1.itemsArray = recomm.collections;
        cell = cell1;
    }

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height;
     RecommsData * recomm = _selectionArray[indexPath.row];
    if (![recomm.type isEqualToString:@"Recomm"]) {
        
        height = 220;
    }
    else{
        height = 50+200 + recomm.short_descHeight+32+10;
    }
    return height;
}

-(void)selection_CollectionCellDidClick:(Selection_CollectionCell *)selection_collectionCell recomm_collection:(Collections_Model *)recomm_collection index:(NSInteger)index
{
    if ([self.delegate respondsToSelector:@selector(lgTableViewDidSelectItem_Collection:recomm_collection:itemIndex:)]) {
        [self.delegate lgTableViewDidSelectItem_Collection:self recomm_collection:recomm_collection itemIndex:index];
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(lgTableViewDidSelectItem_Recomm:recomm:index:)]) {
        [self.delegate lgTableViewDidSelectItem_Recomm:self recomm: _selectionArray[indexPath.row] index:indexPath.row];
    }
}

@end
